package com.example.administrator.myapplication;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText mEtData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEtData=findViewById(R.id.et_data);
    }

    public void save(View view) {
        String data =mEtData.getText().toString();
        //保存SP


        //得到对象编辑器
         SharedPreferences sp= getSharedPreferences("data.sp",MODE_PRIVATE);//获取ｓｐ对象
         SharedPreferences.Editor editor=sp.edit();//从对象sp中获取编辑器edior



        //将数据写入sp
        editor.putString("name",data);

3
        //保存提交commit
        editor.commit();


    }

    public void read(View view) {
        //获取ｓｐ对象
        SharedPreferences sp=getSharedPreferences("data.sp",MODE_PRIVATE);
        //读取内容
         String name= sp.getString("name","");
        TextView tvData=findViewById(R.id.tv_data);
        tvData.setText(name);





    }
}
